package com.timeset.util;

import com.alibaba.fastjson.JSONObject;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @ClassName QiniuUtil
 * @Description
 * @Author szy
 * @Date 2020-05-20 16:13
 */
@Service
public class QiniuUtil {


    // 设置好账号的ACCESS_KEY和SECRET_KEY
    String accessKey = "wBoa0UCuVQWeacKvA_tUVNGsxu9OMzTKicleLmCx";
    String secretKey = "L61AKVDtfSYndGbxl0LJLP3R4tcL9XBUba-ndmVr";
    String bucket = "shoolnews";


    // 密钥配置
    Auth auth = Auth.create(accessKey, secretKey);
    // 构造一个带指定Zone对象的配置类,不同的七云牛存储区域调用不同的zone
    Configuration cfg = new Configuration(Zone.zone1());
    // ...其他参数参考类注释
    UploadManager uploadManager = new UploadManager(cfg);

    // 简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public String getUpToken() {
        return auth.uploadToken(bucket);
    }


    public String saveImage(File file, String fileName) throws IOException {
        try {
            int dotPos = fileName.lastIndexOf(".");
            if (dotPos < 0) {
                return null;
            }
            String fileExt = fileName.substring(dotPos + 1).toLowerCase();
            // 判断是否是合法的文件后缀
            if (!FileUtil.isFileAllowed(fileExt)) {
                return null;
            }
            String randName = UUID.randomUUID().toString().replaceAll("-", "") + "." + fileExt;
            Response res = uploadManager.put(file, randName, getUpToken());
            // 打印返回的信息
            if (res.isOK() && res.isJson()) {
                // 返回这张存储照片的地址
                return "http://qael8oimo.bkt.clouddn.com/" + JSONObject.parseObject(res.bodyString()).get("key").toString();
            } else {
                System.out.println("七牛异常1:" + res.bodyString());
                return null;
            }
        } catch (QiniuException e) {
            // 请求失败时打印的异常的信息
            System.out.println("七牛异常2:" + e.getMessage());
            return null;
        }
    }
    //删除文件
    public int delete(String url) throws QiniuException{
        Configuration cfg = new Configuration(Zone.zone1());//设置华北的服务器
        Auth auth = Auth.create(accessKey, secretKey);
        String key=url.substring(33,url.length());
        String bucketname = "shoolnews";
        System.out.println(bucketname+"....."+key);
        BucketManager bucketManager = new BucketManager(auth, cfg);

        try {
            Response delete = bucketManager.delete(bucketname, key);
            int i=delete.statusCode;
            System.out.println("返回的值：：：：："+i);
            return i;
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            ex.printStackTrace();
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
        return -1;
    }
   }





